package com.sumainfor.pc.epc.service.impl;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.*;
import com.sumainfor.pc.epc.dao.MktFeign.HousingRepairMapper;
import com.sumainfor.pc.epc.dao.OrderMapper;
import com.sumainfor.pc.epc.dao.ReceiveMapper;
import com.sumainfor.pc.epc.dao.RepairMapper;
import com.sumainfor.pc.epc.dao.UtilsMapper;
import com.sumainfor.pc.epc.feign.GetUserInfoFeign;
import com.sumainfor.pc.epc.feign.GmioFeign;
import com.sumainfor.pc.epc.feign.Housingauditfeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.epc.service.RepairService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class RepairServiceImpl implements RepairService{

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log= LoggerFactory.getLogger(RepairServiceImpl.class);
    @Autowired
    private RepairMapper repairMapper;
    @Autowired
    private ZpcMktFeign zpcMktFeign;
    @Autowired
    private ZzuulFeign zzuulFeign;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private HousingRepairMapper HousingRepairMapper;

    @Autowired
    Housingauditfeign Housingauditfeign;

    @Autowired
    GetUserInfoFeign GetUserInfoFeign;

    @Autowired
    ReceiveMapper ReceiveMapper;

    @Autowired
    GmioFeign GmioFeign;

    @Autowired
    UtilsMapper UtilsMapper;

    String RepId=null;

    /**
     * 维修工单池
     * @param params
     * @param pager
     * @return
     */
    @Override
    public JsonResult getRepairList(Map<String, Object> params,Pager pager) {
        PageUtils pageUtils = new PageUtils();
        List<Map<String,Object>> comlist=new ArrayList<Map<String,Object>>();
        //是否有选择省市区条件
        if (FieldCheckUtil.isMapFieldNotNull(params ,"Province")
                || FieldCheckUtil.isMapFieldNotNull(params ,"City")
                || FieldCheckUtil.isMapFieldNotNull(params ,"District")
                || FieldCheckUtil.isMapFieldNotNull(params ,"Street")
                || FieldCheckUtil.isMapFieldNotNull(params ,"ComId")) {

            JsonResult comFeign=zpcMktFeign.getComIdList(params);
            if (comFeign.get("errorCode").equals("0")) {
                comlist=(List<Map<String,Object>>)comFeign.get("result");
                params.put("comlist",comlist);
            }
        }
        pager.setPagerNecessary(params, pager);
        System.out.println("查询的参数:"+params);
        List<Map<String, Object>> epcList = repairMapper.getRepairList(params);
        Integer epcCount=repairMapper.getRepairListCount(params);
        if(epcList.size()>0) {
            //保存维修工单的房源编号
            List<String> houIdList = new ArrayList<String>();
            //保存管家报修人编号
            List<String> uidList=new ArrayList<String>();
            //保存客户报修人编号
            List<String> tidList=new ArrayList<String>();
            //保存维修师傅的编号
            List<String> sysList=new ArrayList<String>();
            for (Map<String, Object> map : epcList) {
                if (map.get("HouCon") != null) {
                    //根据 HouId查询房源信息
                    houIdList.add(map.get("HouCon").toString());
                }
                if(map.get("RepType")!=null){
                    //RepType 报修人类型 （1.普通用户，2.管家）
                    if (map.get("RepType").toString().equals("2")){
                        if(map.get("RepairsId") !=null){
                            uidList.add(map.get("RepairsId").toString());
                        }
                    }else{
                        if(map.get("RepairsId") !=null){
                            tidList.add(map.get("RepairsId").toString());
                        }
                    }
                }
                if (map.get("SysUserId")!=null){
                    sysList.add(map.get("SysUserId").toString());
                }
            }
            //根据房源编号到市场部查询房源基本信息
            if(uidList.size()>0 || sysList.size()>0){//如果报修人是管家的 编号 集合大于0就去网关查询
                params.put("uidList",uidList);
                params.put("sysList",sysList);
                JsonResult zuulResult=zzuulFeign.getRuserList(params);
                System.out.println("zuulResult:"+zuulResult);
                Map<String,Object> roMap=(Map<String,Object>)zuulResult.get("result");
                //报修人信息
                List<Map<String,Object>> zuulList=(List<Map<String,Object>>)roMap.get("rList");
                //受理人信息
                List<Map<String,Object>> zuuolList=(List<Map<String,Object>>)roMap.get("oList");
                System.out.println("zuulList:"+zuulList);
                System.out.println("epcList:"+epcList);
                if (zuulResult.get("errorCode").equals("0")) {
                    for (Map<String, Object> map : epcList) {
                        if (map.get("RepairsId")!=null) {
                            for (Map<String, Object> map2 : zuulList) {
                                if (map2.get("user_id")!=null){
                                    if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "user_id"))) {
                                        map.putAll(map2);
                                    }
                                }
                            }
                        }
                    }
                    for (Map<String, Object> map : epcList) {
                        if (map.get("SysUserId")!=null) {
                            for (Map<String, Object> map2 : zuuolList) {
                                if (MapUtils.getString(map, "SysUserId").equals(MapUtils.getString(map2, "user_id"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }
                }
            }
            //把房源配置主表编号传递到市场部
            params.put("idList",houIdList);
            System.out.println("tidList：租客编号数组"+tidList);
            //把租客编号传递到市场部
            params.put("TenantList",tidList);
            System.out.println("传递到市场部的参数:"+params);
            JsonResult result = zpcMktFeign.getCommunityList(params, pager);
            //房源信息
            List<Map<String, Object>> mktList = (List<Map<String, Object>>) result.get("result");
            System.out.println("mktList:"+mktList);
            System.out.println("mktList长度："+mktList.size());
            if (result.get("errorCode").equals("0")) {
                //租客报修人信息
                List<Map<String, Object>> tmktList = (List<Map<String, Object>>) result.get("tenList");
                System.out.println("resuyktmktList"+mktList);
                System.out.println("resuykt"+tmktList);
                if (tmktList!=null) {
                    for (Map<String, Object> map : epcList) {
                        if (map.get("RepairsId")!=null){
                            for (Map<String, Object> map2 : tmktList) {
                                if (map2.get("TenId")!=null) {
                                    if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "TenId"))) {
                                        map.putAll(map2);
                                    }
                                }
                            }
                        }

                    }
                }
                for (Map<String, Object> map : epcList) {
                    if (map.get("HouCon")!=null){
                        for (Map<String, Object> map2 : mktList) {
                            if (map2.get("HouCon")!=null) {
                                if (MapUtils.getString(map, "HouCon").equals(MapUtils.getString(map2, "HouCon"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }
                }
            }
            return pageUtils.getJsonResult(epcList, params,epcCount);
        }
        return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,0);
    }
    /**
     * 查询我的工单的列表
     * @param params
     * @return
     */
    @Override
    public JsonResult getRepairMeList(Map<String, Object> params,Pager pager) {
        JsonResult zuulResult=new JsonResult();
        PageUtils pageUtils = new PageUtils();
        String uid=ContextUtils.getUserId();
        //判断是否能获取到当前登录用户id
        System.out.println("uid:"+uid);
        if (StringUtils.isBlank(uid)){
            return zuulResult.putFailed("服务器繁忙");
        }
        params.put("SysUserId",uid);//当前登录人的编号
        List<Map<String,Object>> comlist=new ArrayList<Map<String,Object>>();
        //是否有选择省市区条件
        if (FieldCheckUtil.isMapFieldNotNull(params ,"Province")
                || FieldCheckUtil.isMapFieldNotNull(params ,"City")
                || FieldCheckUtil.isMapFieldNotNull(params ,"District")
                || FieldCheckUtil.isMapFieldNotNull(params ,"Street")
                || FieldCheckUtil.isMapFieldNotNull(params ,"ComId")
        ){
            JsonResult comFeign=zpcMktFeign.getComIdList(params);
            if (comFeign.get("errorCode").equals("0")) {
                comlist=(List<Map<String,Object>>)comFeign.get("result");
                params.put("comlist",comlist);
            }
        }
        pager.setPagerNecessary(params, pager);
        List<Map<String, Object>> epcList = repairMapper.getRepairMeList(params);
        System.out.println("epcList:"+epcList);
        Integer epcCount=repairMapper.getRepairMeListCount(params);
        if(epcList.size()>0) {
            //保存维修工单的房源编号
            List<String> houIdList = new ArrayList<String>();
            //保存管家报修人编号
            List<String> uidList=new ArrayList<String>();
            //保存客户报修人编号
            List<String> tidList=new ArrayList<String>();
            //保存维修师傅的编号
            List<String> sysList=new ArrayList<String>();
            for (Map<String, Object> map : epcList) {
                if (map.get("HouCon") != null) {
                    //根据 HouId查询房源信息
                    houIdList.add(map.get("HouCon").toString());
                }
                if(map.get("RepType")!=null){
                    //是否通过管家巡房发起  0.否   1是'
                    if (map.get("RepType").toString().equals("2")){
                        if(map.get("RepairsId") !=null){
                            uidList.add(map.get("RepairsId").toString());
                        }
                    }else{
                        if(map.get("RepairsId") !=null){
                            tidList.add(map.get("RepairsId").toString());
                        }
                    }
                }
                if (map.get("SysUserId")!=null){
                    sysList.add(map.get("SysUserId").toString());
                }
            }
            System.out.println("传递到网关的参数:"+uidList);
            //根据房源编号到市场部查询房源基本信息
            if(uidList.size()>0 || sysList.size()>0){//把租客的编号传到市场部查询租客即报修人的信息
                params.put("uidList",uidList);
                params.put("sysList",sysList);
                zuulResult=zzuulFeign.getRuserList(params);
                System.out.println("测试记录:"+zuulResult);
                Map<String,Object> roMap=(Map<String,Object>)zuulResult.get("result");
                //报修人信息
                List<Map<String,Object>> zuulList=(List<Map<String,Object>>)roMap.get("rList");
                //受理人信息
                List<Map<String,Object>> zuuolList=(List<Map<String,Object>>)roMap.get("oList");
                if (zuulResult.get("errorCode").equals("0")) {
                    for (Map<String, Object> map : epcList) {
                        for (Map<String, Object> map2 : zuulList) {
                            if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "user_id"))) {
                                map.putAll(map2);
                            }
                        }
                    }
                    for (Map<String, Object> map : epcList) {
                        for (Map<String, Object> map2 : zuuolList) {
                            if (MapUtils.getString(map, "SysUserId").equals(MapUtils.getString(map2, "user_id"))) {
                                map.putAll(map2);
                            }
                        }
                    }
                }
            }
            //把房源详表编号传递到市场部
            params.put("idList",houIdList);
            //把租客编号传递到市场部
            params.put("TenantList",tidList);
            System.out.println("传递到市场部的参数:"+params);
            JsonResult result = zpcMktFeign.getCommunityList(params, pager);
            //房源信息
            List<Map<String, Object>> mktList = (List<Map<String, Object>>) result.get("result");
            if (result.get("errorCode").equals("0")) {
                //租客报修人信息
                List<Map<String, Object>> tmktList = (List<Map<String, Object>>) result.get("tenList");
                System.out.println("resuyktmktList"+mktList);
                System.out.println("resuykt"+tmktList);
                if (tmktList!=null) {
                    for (Map<String, Object> map : epcList) {
                        if (map.get("RepairsId")!=null){
                            for (Map<String, Object> map2 : tmktList) {
                                if (map2.get("TenId")!=null) {
                                    if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "TenId"))) {
                                        map.putAll(map2);
                                    }
                                }
                            }
                        }

                    }
                }
                for (Map<String, Object> map : epcList) {
                    if (map.get("HouCon")!=null) {
                        for (Map<String, Object> map2 : mktList) {
                            if (map2.get("HouCon")!=null) {
                                if (MapUtils.getString(map, "HouCon").equals(MapUtils.getString(map2, "HouCon"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }
                }
            }
            return pageUtils.getJsonResult(epcList, params,epcCount);
        }
        return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,0);
    }
    /**
     *  查询工作台 重要状态提醒 进行中工单 大修 紧急 小修
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> getRepairCountByUrgency(Map<String, Object> params) {
        return repairMapper.getRepairCountByUrgency(params);
    }
    /**
     * 点击受理 修改维修工单的状态
     * @param params
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean  updateRepState(Map<String, Object> params) {
        boolean result=false;
        lock.lock();
        try{
            try {
                params.put("RepState","2");//1:未接单 2:已接单
                params.put("by", ContextUtils.getUserId());//操作人
                params.put("term", "3");// 更新端 Update端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonUpdateParms(params);
                int count = repairMapper.updateRepState(params);
                if (count > 0) {
                    params.put("term", "3");// 更新端 Update端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                    params.put("fnc",
                            this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                    ParamsCommon.commonInsertParms(params);
                    params.put("OrdTime",params.get("instm"));//我的工单接单时间
                    params.put("SysUserId", ContextUtils.getUserId());
                    params.put("IfCoerce",0);//是否强派   0正常   1强派',
                    int cou=orderMapper.addOrder(params);//新增我的工单
                    if (cou>0){
                        result=true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }
    /**
     * 根据领用单编号去查询领用单列表
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> getReceiveByRepId(Map<String, Object> params) {
        return repairMapper.getReceiveByRepId(params);
    }

    /**
     * 查询待接维修工单数
     * @return
     */
    @Override
    public Integer getRepairDaiCountList() {
        return repairMapper.getRepairDaiCountList();
    }


    /**
     * 查询维修跟踪列表 待受理
     * @param params
     * @return
     */
    @Override
    public JsonResult getRepairListd(Map<String, Object> params, Pager pager) {
        PageUtils pageUtils = new PageUtils();
        List<Map<String,Object>> comlist=new ArrayList<Map<String,Object>>();
        //是否有选择省市区条件
        if (FieldCheckUtil.isMapFieldNotNull(params ,"Province")
                || FieldCheckUtil.isMapFieldNotNull(params ,"City")
                || FieldCheckUtil.isMapFieldNotNull(params ,"District")
                || FieldCheckUtil.isMapFieldNotNull(params ,"Street")
                || FieldCheckUtil.isMapFieldNotNull(params ,"ComId")
        ){
            JsonResult comFeign=zpcMktFeign.getComIdList(params);
            if (comFeign.get("errorCode").equals("0")) {
                comlist=(List<Map<String,Object>>)comFeign.get("result");
                params.put("comlist",comlist);
            }
        }
        //如果模糊搜索的字段有楼号, 用项目id和楼号室号去查询所有的房源id
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"BuilDing"})) {
            List<String> houseIdList = zpcMktFeign.getHouseIdListByBuildAndRoom(params);
            if (houseIdList != null && houseIdList.size() > 0) {
                params.put("houIdList", houseIdList);
            }
        }
        pager.setPagerNecessary(params, pager);
        //查询工程部的维修工单信息和房源编号
        //保存工程部查询到的id集合
        List<String> idList=new ArrayList<String>();
        List<Map<String,Object>> list=repairMapper.getRepairListFromGmiod(params);
        Integer count=repairMapper.getRepairListFromGmiodCount(params);
        List<Map<String,Object>> ztList=new ArrayList<Map<String,Object>>();//保存的编号
        for (Map<String,Object> map: list){
            if(map.get("HouCon")!=null){
                idList.add(map.get("HouCon").toString());
            }
            if (map.get("RepairsId")!=null){
                ztList.add(map);
            }
        }
        //如果维修工单是管家发起的
        if(params.get("RepType").toString().equals("2")) {//1.普通用户，2.管家
            Map<String, Object> dataMap = new HashMap<String, Object>();//传递的参数
            dataMap.put("RepState",params.get("RepState"));
            if (ztList.size() > 0) {
                dataMap.put("zuulList", ztList);
                //查询服务管家发起的维修申请
                JsonResult zuulResult = zzuulFeign.getRepairUserList(dataMap);
                Map<String,Object> zuulMap=(Map<String, Object>) zuulResult.get("result");
                ztList = (List<Map<String, Object>>) zuulMap.get("rList");
                System.out.println("网关查询的到的参数:" + zuulResult);
            }
        }
        if(idList.size()>0) {
            pager.setPagerNecessary(params, pager);
            //把租客的编号一起传递到市场部查询信息
            params.put("tenantList",ztList);
            params.put("idList",idList);
            System.out.println("传递到市场部的参数:" + params);
            JsonResult mktResult = zpcMktFeign.getHouInfoListForRepair(params);
            System.out.println("市场部查询到信息:" + mktResult);
            //把查询到的租客信息保存到集合中
            List<Map<String, Object>> tenList = (List<Map<String, Object>>) mktResult.get("tList");
            List<Map<String, Object>> mktlist = (List<Map<String, Object>>) mktResult.get("result");
            for (Map<String, Object> map : list) {
                if(map.get("HouCon")!=null) {
                    for (Map<String, Object> map2 : mktlist) {
                        if(map2.get("HouCon")!=null) {
                            if (MapUtils.getString(map, "HouCon").equals(MapUtils.getString(map2, "HouCon"))) {
                                map.putAll(map2);
                            }
                        }
                    }
                }
            }
            if(params.get("RepType").toString().equals("1")){//1.普通用户，2.管家
                //把网关查询到的信息和集合匹配
                for (Map<String, Object> map : list) {
                    if (map.get("RepairsId")!=null){
                        for (Map<String, Object> map2 : tenList) {
                            if (map2.get("TenId")!=null) {
                                if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "TenId"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }

                }
            }else{
                //把网关查询到的信息和集合匹配
                for (Map<String, Object> map : list) {
                    if (map.get("RepairsId")!=null) {
                        if (map.get("RepairsId")!=null) {
                            for (Map<String, Object> map2 : ztList) {
                                if (map2.get("user_id")!=null) {
                                    if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "user_id"))) {
                                        map.putAll(map2);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return pageUtils.getJsonResult(list, params,count);
        }
        return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,0);
    }
    //*

    /*
     * 查询维修跟踪列表 已受理
     */
    @Override
    public JsonResult getRepairListy(Map<String, Object> params, Pager pager) {
        PageUtils pageUtils = new PageUtils();
        List<Map<String,Object>> comlist=new ArrayList<Map<String,Object>>();
        //是否有选择省市区条件
        if (FieldCheckUtil.isMapFieldNotNull(params ,"Province")
                || FieldCheckUtil.isMapFieldNotNull(params ,"City")
                || FieldCheckUtil.isMapFieldNotNull(params ,"District")
                || FieldCheckUtil.isMapFieldNotNull(params ,"Street")
                || FieldCheckUtil.isMapFieldNotNull(params ,"ComId")
        ){
            JsonResult comFeign=zpcMktFeign.getComIdList(params);
            if (comFeign.get("errorCode").equals("0")) {
                comlist=(List<Map<String,Object>>)comFeign.get("result");
                params.put("comlist",comlist);
            }
        }
        //如果模糊搜索的字段有楼号, 用项目id和楼号室号去查询所有的房源id
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"BuilDing"})) {
            List<String> houseIdList = zpcMktFeign.getHouseIdListByBuildAndRoom(params);
            if (houseIdList != null && houseIdList.size() > 0) {
                params.put("houIdList", houseIdList);
            }
        }
        pager.setPagerNecessary(params, pager);
        //查询工程部的维修工单信息和房源编号
        //保存工程部查询到的id集合
        List<String> idList=new ArrayList<String>();
        List<Map<String,Object>> list=repairMapper.getRepairListFromGmioy(params);
        Integer count=repairMapper.getRepairListFromGmioyCount(params);
        List<Map<String,Object>> ztList=new ArrayList<Map<String,Object>>();//保存的编号
        List<Map<String,Object>> otList=new ArrayList<Map<String,Object>>();//保存的编号
        for (Map<String,Object> map: list){
            if(map.get("HouCon")!=null){
                idList.add(map.get("HouCon").toString());
            }
            if (map.get("RepairsId")!=null){
                ztList.add(map);
            }
        }
        //如果维修工单是管家发起的
        if(params.get("RepType").toString().equals("2")) {//1.普通用户，2.管家
            Map<String, Object> dataMap = new HashMap<String, Object>();//传递的参数
            dataMap.put("RepState",params.get("RepState"));
            if (ztList.size() > 0) {
                dataMap.put("zuulList", ztList);
                //查询服务管家发起的维修申请
                JsonResult zuulResult = zzuulFeign.getRepairUserList(dataMap);
                Map<String,Object> zuulMap=(Map<String, Object>) zuulResult.get("result");
                ztList = (List<Map<String, Object>>) zuulMap.get("rList");
                otList = (List<Map<String, Object>>) zuulMap.get("oList");
                System.out.println("网关查询的到的参数:" + zuulResult);
            }
        }else{
            Map<String, Object> dataMap = new HashMap<String, Object>();//传递的参数
            if (ztList.size() > 0) {
                dataMap.put("zuulList", ztList);
                //查询服务管家发起的维修申请
                JsonResult zuulResult = zzuulFeign.getRepairOuserList(dataMap);
                otList=(List<Map<String, Object>>)zuulResult.get("result");
                System.out.println("网关查询的到的参数:" + zuulResult);
            }
        }
        if(idList.size()>0) {
            pager.setPagerNecessary(params, pager);
            //把租客的编号一起传递到市场部查询信息
            params.put("tenantList",ztList);
            params.put("idList",idList);
            System.out.println("传递到市场部的参数:" + params);
            JsonResult mktResult = zpcMktFeign.getHouInfoListForRepair(params);
            System.out.println("市场部查询到信息:" + mktResult);
            //把查询到的租客信息保存到集合中
            List<Map<String, Object>> tenList = (List<Map<String, Object>>) mktResult.get("tList");
            List<Map<String, Object>> mktlist = (List<Map<String, Object>>) mktResult.get("result");
            for (Map<String, Object> map : list) {
                if(map.get("HouCon")!=null) {
                    for (Map<String, Object> map2 : mktlist) {
                        if(map2.get("HouCon")!=null) {
                            if (MapUtils.getString(map, "HouCon").equals(MapUtils.getString(map2, "HouCon"))) {
                                map.putAll(map2);
                            }
                        }
                    }
                }
            }
            if(params.get("RepType").toString().equals("1")){//1.普通用户，2.管家
                //把网关查询到的信息和集合匹配
                for (Map<String, Object> map : list) {
                    if (map.get("RepairsId")!=null){
                        for (Map<String, Object> map2 : tenList) {
                            if (map2.get("TenId")!=null) {
                                if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "TenId"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }

                }
            }else{
                //把网关查询到的信息和集合匹配
                for (Map<String, Object> map : list) {
                    if (map.get("RepairsId")!=null) {
                        for (Map<String, Object> map2 : ztList) {
                            if (map2.get("user_id")!=null) {
                                if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "user_id"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }
                }
            }
            if(otList.size()>0 && otList!=null){
                for (Map<String, Object> map : list) {
                    if (map.get("RepairsId")!=null) {
                        for (Map<String, Object> map2 : otList) {
                            if (map2.get("OsId")!=null) {
                                if (MapUtils.getString(map, "SysUserId").equals(MapUtils.getString(map2, "OsId"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }
                }
            }
            return pageUtils.getJsonResult(list, params,count);
        }
        return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,0);
    }
    /**
     * 查询维修跟踪列表 已维修
     * @param params
     * @return
     */
    @Override
    public JsonResult getRepairListw(Map<String, Object> params, Pager pager) {
        PageUtils pageUtils = new PageUtils();
        List<Map<String,Object>> comlist=new ArrayList<Map<String,Object>>();
        //是否有选择省市区条件
        if (FieldCheckUtil.isMapFieldNotNull(params ,"Province")
                || FieldCheckUtil.isMapFieldNotNull(params ,"City")
                || FieldCheckUtil.isMapFieldNotNull(params ,"District")
                || FieldCheckUtil.isMapFieldNotNull(params ,"Street")
                || FieldCheckUtil.isMapFieldNotNull(params ,"ComId")
        ){
            JsonResult comFeign=zpcMktFeign.getComIdList(params);
            if (comFeign.get("errorCode").equals("0")) {
                comlist=(List<Map<String,Object>>)comFeign.get("result");
                params.put("comlist",comlist);
            }
        }
        //如果模糊搜索的字段有楼号, 用项目id和楼号室号去查询所有的房源id
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"BuilDing"})) {
            List<String> houseIdList = zpcMktFeign.getHouseIdListByBuildAndRoom(params);
            if (houseIdList != null && houseIdList.size() > 0) {
                params.put("houIdList", houseIdList);
            }
        }
        pager.setPagerNecessary(params, pager);
        //查询工程部的维修工单信息和房源编号
        //保存工程部查询到的id集合
        List<String> idList=new ArrayList<String>();
        List<Map<String,Object>> list=repairMapper.getRepairListFromGmiow(params);
        Integer count=repairMapper.getRepairListFromGmiowCount(params);
        List<Map<String,Object>> ztList=new ArrayList<Map<String,Object>>();//保存的编号
        List<Map<String,Object>> otList=new ArrayList<Map<String,Object>>();//保存的编号
        for (Map<String,Object> map: list){
            if(map.get("HouCon")!=null){
                idList.add(map.get("HouCon").toString());
            }
            if (map.get("RepairsId")!=null){
                ztList.add(map);
            }
        }
        //如果维修工单是管家发起的
        if(params.get("RepType").toString().equals("2")) {//1.普通用户，2.管家
            Map<String, Object> dataMap = new HashMap<String, Object>();//传递的参数
            dataMap.put("RepState",params.get("RepState"));
            if (ztList.size() > 0) {
                dataMap.put("zuulList", ztList);
                //查询服务管家发起的维修申请
                JsonResult zuulResult = zzuulFeign.getRepairUserList(dataMap);
                Map<String,Object> zuulMap=(Map<String, Object>) zuulResult.get("result");
                ztList = (List<Map<String, Object>>) zuulMap.get("rList");
                otList = (List<Map<String, Object>>) zuulMap.get("oList");
                System.out.println("网关查询的到的参数:" + zuulResult);
            }
        }else{
            Map<String, Object> dataMap = new HashMap<String, Object>();//传递的参数
            if (ztList.size() > 0) {
                dataMap.put("zuulList", ztList);
                //查询服务管家发起的维修申请
                JsonResult zuulResult = zzuulFeign.getRepairOuserList(dataMap);
                otList=(List<Map<String, Object>>)zuulResult.get("result");
                System.out.println("网关查询的到的参数:" + zuulResult);
            }
        }
        if(idList.size()>0) {
            pager.setPagerNecessary(params, pager);
            //把租客的编号一起传递到市场部查询信息
            params.put("tenantList",ztList);
            params.put("idList",idList);
            System.out.println("传递到市场部的参数:" + params);
            JsonResult mktResult = zpcMktFeign.getHouInfoListForRepair(params);
            System.out.println("市场部查询到信息:" + mktResult);
            //把查询到的租客信息保存到集合中
            List<Map<String, Object>> tenList = (List<Map<String, Object>>) mktResult.get("tList");
            List<Map<String, Object>> mktlist = (List<Map<String, Object>>) mktResult.get("result");
            for (Map<String, Object> map : list) {
                for (Map<String, Object> map2 : mktlist) {
                    if (MapUtils.getString(map, "HouCon").equals(MapUtils.getString(map2, "HouCon"))) {
                        map.putAll(map2);
                    }
                }
            }
            System.out.println("tenListasdasdasd："+tenList);
            if(params.get("RepType").toString().equals("1")){//1.普通用户，2.管家
                //把网关查询到的信息和集合匹配
                for (Map<String, Object> map : list) {
                    for (Map<String, Object> map2 : tenList) {
                        if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "TenId"))) {
                            map.putAll(map2);
                        }
                    }
                }
            }else{
                //把网关查询到的信息和集合匹配
                for (Map<String, Object> map : list) {
                    for (Map<String, Object> map2 : ztList) {
                        if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "user_id"))) {
                            map.putAll(map2);
                        }
                    }
                }
            }
            if(otList.size()>0 && otList!=null){
                for (Map<String, Object> map : list) {
                    for (Map<String, Object> map2 : otList) {
                        if (MapUtils.getString(map, "SysUserId").equals(MapUtils.getString(map2, "OsId"))) {
                            map.putAll(map2);
                        }
                    }
                }
            }
            return pageUtils.getJsonResult(list, params,count);
        }
        return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,0);
    }

    /**
     *查询同房源的其他维修工单列表
     */
    @Override
    public JsonResult getRepairHouList(Map<String,Object> params){
        JsonResult result=new JsonResult();
        if (params.get("HouId")==null || params.get("HouCon")==null){
         result.putFailed("参数不正确");
        }
        List<Map<String,Object>> list=repairMapper.getRepairHouList(params);
        if (list.size()>0){
            result.put(list);
        }else{
          //  result.putFailed("服务器繁忙");
            result.put(new ArrayList<Map<String,Object>>());
        }
        return result;
    }
    /**
     * 租客生成报修单
     * @param params
     * @return
     */
    @Override
    public Boolean createRepair(Map<String, Object> params) {
        String tenId = (String) params.get("RepairsId");
        params.put("RepType",1);  // 报修人类型为普通用户
        //String tenId = ContextUtils.getWxInfo().getTenId();
        if(HousingRepairMapper.AddRepair(params)>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 功能描述:
     * 工程助理维修工单池
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/9 11:18
     */
    public List<Map<String,Object>>getManagerRepairList(Map<String,Object>params){
        List<Map<String,Object>>getManagerRepairList=repairMapper.getManagerRepairList(params);
        //所有用户名称
        JsonResult jsonResult=zzuulFeign.getAllUserIdAndName();
        List<Map<String,Object>>getAllUserIdAndName=(List<Map<String,Object>>)jsonResult.getResult();
        //根据报修人编号合并用户资料
        getManagerRepairList= ListMapMergeUtil.ListMergeUtils(getManagerRepairList,getAllUserIdAndName,"userId");

        //房源资料
        JsonResult jsonResult1=Housingauditfeign.getAllHousingConLists();
        List<Map<String,Object>>getAllHousingConList=(List<Map<String,Object>>)jsonResult1.getResult();
        getManagerRepairList= ListMapMergeUtil.ListMergeUtils(getManagerRepairList,getAllHousingConList,"HouCon");


        return getManagerRepairList;
    }

    /**
     * 功能描述:
     * 工程助理维修工单池总数
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/9 11:18
     */
    public Integer getManagerRepairListCount(Map<String,Object>params){
        return repairMapper.getManagerRepairListCount(params);
    }

    /**
     * 功能描述:
     * 工程助理强派工单
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/9 18:45
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean AddRepair(Map<String,Object>params){
        boolean result=false;
        lock.lock();
        try{
            //所有新增处理
            try {
                Map<String,Object>commonInsertParms=new HashMap<String,Object>();
                commonInsertParms.put("by", ContextUtils.getUserId());//操作人
                commonInsertParms.put("term", "2");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                commonInsertParms.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(commonInsertParms);
                params.put("OrdTime",ConvertDateTime.getCurrentTime());
                params.put("IfCoerce",1);//是否强派   0正常   1强派
                repairMapper.AddRepair(params);
                params.put("RepState",3);//状态   1待接单   2已接单  3被强派
                repairMapper.updateRepState(params);
                result=true;

                //通知
                CustomMap data = CustomMap.create("MeType", 8)
                        .put("Title", "被强派工单")
                        .put("Content", HtmlUtil.stringToHTML("您收到一条新的强派工单！", "", Color.blue))
                        .put("by", ContextUtils.getUserId())
                        .put("term", 3);
                zzuulFeign.AddMessage(data);

            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }

    /**
     * 功能描述:
     * 根据维修工单编号获取维修资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/10 11:17
     */
    @Override
    public Map<String,Object>getRepairMap(Map<String,Object>params){
        Map<String,Object>getRepairMap=repairMapper.getRepairMap(params);

        //根据维修单房源编号获取房源资料
        if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"HouId"})){
            JsonResult jsonResult=Housingauditfeign.getRepairMaps(new HashMap<String,Object>(){{
                put("HouId",getRepairMap.get("HouId"));
            }});
            if(jsonResult.isSuccess()==true){
                Map<String,Object>getHousingMap=(Map<String,Object>)jsonResult.getResult();
                //如果房源管家编号不为空
                if(FieldCheckUtil.isMapFieldNotNull(getHousingMap,new String[]{"SysUserId"})){
                    //获取房源管家
                    JsonResult jsonResult2=GetUserInfoFeign.getUserMap(new HashMap<String,Object>(){{
                        put("UserId",getHousingMap.get("SysUserId"));
                    }});
                    if(jsonResult2.isSuccess()==true){
                        Map<String,Object>getUserMap=(Map<String,Object>)jsonResult2.getResult();
                        getHousingMap.put("SysName",getUserMap.get("UserName"));
                        getHousingMap.put("SysPhone",getUserMap.get("Mobile"));
                        getHousingMap.put("areaName",getUserMap.get("areaName"));
                    }else{
                        getHousingMap.put("SysName",null);
                        getHousingMap.put("SysPhone",null);
                        getHousingMap.put("areaName",null);
                    }
                }
                getRepairMap.put("HousingMap",getHousingMap);
            }else{
                getRepairMap.put("HousingMap",new HashMap<String,Object>());
            }
        }else{
            getRepairMap.put("HousingMap",new HashMap<String,Object>());
        }

        //如果维修师傅编号不为空
        if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"ordUserId"})){
            //获取维修师傅信息
            JsonResult jsonResult1=GetUserInfoFeign.getUserMap(new HashMap<String,Object>(){{
                put("UserId",getRepairMap.get("ordUserId"));
            }});
            if(jsonResult1.isSuccess()==true){
                Map<String,Object>getUserMap=(Map<String,Object>)jsonResult1.getResult();
                getRepairMap.put("ordImg",getUserMap.get("UserImg"));
                getRepairMap.put("ordName",getUserMap.get("UserName"));
                getRepairMap.put("ordPhone",getUserMap.get("Mobile"));
            }else{
                getRepairMap.put("ordImg",null);
                getRepairMap.put("ordName",null);
                getRepairMap.put("ordPhone",null);
            }
        }



        //报修人为管家
        if(getRepairMap.get("RepType").equals(2)||getRepairMap.get("RepType").toString()=="2"){
            //如果报修人编号
            if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"RepairsId"})){
                //获取房源管家
                JsonResult jsonResult1=GetUserInfoFeign.getUserMap(new HashMap<String,Object>(){{
                    put("UserId",getRepairMap.get("RepairsId"));
                }});
                if(jsonResult1.isSuccess()==true){
                    Map<String,Object>getUserMap=(Map<String,Object>)jsonResult1.getResult();
                    getRepairMap.put("RepairsName",getUserMap.get("UserName"));
                }else{
                    getRepairMap.put("RepairsName",null);
                }
            }

        }else{//报修人为租客
            //如果有报修人编号
            if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"RepairsId"})){
                //获取租客资料
                JsonResult jsonResult1=Housingauditfeign.getTenantMap(new HashMap<String,Object>(){{
                    put("TenId",getRepairMap.get("RepairsId"));
                }});
                if(jsonResult1.isSuccess()==true){
                    Map<String,Object>getUserMap=(Map<String,Object>)jsonResult1.getResult();
                    getRepairMap.put("RepairsName",getUserMap.get("TenName"));
                }else{
                    getRepairMap.put("RepairsName",null);
                }
            }
        }

        //是否接单
        if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"RepState"})){
            String RepState=getRepairMap.get("RepState").toString();
            switch (RepState){
                case "1"://待接单
                    break;
                case "2"://已接单
                    //是否有收费字段
                    if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"IfCost"})){
                        //收费，根据账单编号去运营部账单查询账单资料
                        if(getRepairMap.get("IfCost").toString()=="1"||getRepairMap.get("IfCost").equals("1")){
                            //维修账单是否为空
                            if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"OrdBiId"})){
                                JsonResult jsonResult1=GmioFeign.getRepairBillMap(new HashMap<String,Object>(){{
                                    put("BiId",getRepairMap.get("OrdBiId"));
                                }});
                                Map<String,Object>getRepairBillMap=(Map<String,Object>)jsonResult1.getResult();
                                getRepairMap.put("orderBill",getRepairBillMap);
                            }else{
                                getRepairMap.put("orderBill",new HashMap<String,Object>());
                            }
                        }else{//不收费
                            getRepairMap.put("orderBill",new HashMap<String,Object>());
                        }
                    }
                    break;
                case "3"://被强派
                    //是否有收费字段
                    if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"IfCost"})){
                        //收费，根据账单编号去运营部账单查询账单资料
                        if(getRepairMap.get("IfCost").toString()=="1"||getRepairMap.get("IfCost").equals("1")){
                            //维修账单是否为空
                            if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"OrdBiId"})){
                                JsonResult jsonResult1=GmioFeign.getRepairBillMap(new HashMap<String,Object>(){{
                                    put("BiId",getRepairMap.get("OrdBiId"));
                                }});
                                Map<String,Object>getRepairBillMap=(Map<String,Object>)jsonResult1.getResult();
                                getRepairMap.put("orderBill",getRepairBillMap);
                            }else{
                                getRepairMap.put("orderBill",new HashMap<String,Object>());
                            }
                        }else{//不收费
                            getRepairMap.put("orderBill",new HashMap<String,Object>());
                        }
                    }
                    break;
            }
        }


        //然后查询我的工单是否有领用单
        if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"RecId"})){
            //领用单数据
            List<Map<String,Object>>getRepairReceiveList=ReceiveMapper.getRepairReceiveList(new HashMap<String,Object>(){{
                put("RecId",getRepairMap.get("RecId"));
            }});
            if(getRepairReceiveList.size()>0){
                getRepairMap.put("receiveList",getRepairReceiveList);
            }else{
                getRepairMap.put("receiveList",new ArrayList<Map<String,Object>>());
            }
        }else{//领用单为空
            getRepairMap.put("receiveList",new ArrayList<Map<String,Object>>());
        }

        //报修图片
        if(FieldCheckUtil.isMapFieldNotNull(getRepairMap,new String[]{"TroubleFile"})){
            String[] TroubleFile=getRepairMap.get("TroubleFile").toString().split(",");
            getRepairMap.put("TroubleFile",TroubleFile);
        }else{
            getRepairMap.put("TroubleFile",new String[]{});
        }
        return getRepairMap;
    }
    /**
     * 修改转租申请状态为已上架
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult updateOrdState(Map<String,Object> params){
        JsonResult result=new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {
                if (params.get("RepId")==null){
                    return result.putFailed("参数不正确");
                }
                params.put("OrdState",2);//工单状态   1待维修   2已维修
                params.put("by", ContextUtils.getUserId());//操作人
                params.put("term", "3");// 更新端 Update端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonUpdateParms(params);
                Integer count=repairMapper.updateOrdState(params);
                params.put("FinishTime",ConvertDateTime.getCurrentTime());
                Integer coutOne = repairMapper.updateTcepcOrder(params);
                if (count>0&&coutOne>0){
                    result.putSuccess("操作成功");
                }else{
                    result.putFailed("服务器繁忙");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            } catch (Exception e) {
                result.putFailed("服务器繁忙");
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(ConvertDateTime.getCurrentTime());
    }
}
